Sh.Parser.CommandLine = new Class({
	initialize : function (engine) {
		this.engine = engine;
	},
	parseCommand : function (cmd) {
		this.cmd = cmd;
		this.commands = [];
		this.parts = new App.Parser.UserText(cmd).get();
		this.parse();
	},
	get : function () {
		return this.commands;
	},
	start : function () {
		if (this.commands) {
			this.commands[0].run()
		}
	},
	parse : function () {
		this.parts.each(function (part, i) {
			this.part  = part;
			this.index = i;
			this.parsePart();
		}.bind(this));
		return this;
	},
	parsePart : function () {
		var p = this.part;
		if (p.type == 'string') {
			this.addString();
		} else if (p.type == 'escape') {
			throw 'Unexpected Escape character in ' + this.index;
		} else if (p.type == 'operand') {
			this.parseOperand(p.value);
		} else {
			throw 'Unknown character type in ' + this.index;
		}
	},
	parseOperand : function (value) {
		switch (value) {
			case '||' :
			case ';' :
			case '&&' : {
				this.currentCommand.ifSuccess(
					this.createCommand()
				)
			} break;
			case '>>' :
			case '>' : {
				if (this.currentCommand.send) {
					throw 'Unexpected ' + value + 'in ' + this.index;
				}
				this.currentCommand
					.sendResult(
						value == '>' ? 'force' : 'normal'
					);
			} break;
			case '|' : {
				this.currentCommand.pipe(this.createCommand())
			} break;
			default : {
				throw 'Unknown operand in ' + this.index;
			}
		}
	},
	createCommand : function () {
		this.currentCommand = new App.Parser.Command(this)
		this.commands.push(this.currentCommand);
		return this.currentCommand;
	},
	addString : function () {
		if (this.currentCommand) {
			this.currentCommand
				.addString(this.part.value)
		} else {
			this.createCommand()
				.setName(this.part.value);
		}
	}
})